package com.example.hellodemo.adapter;

import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.example.hellodemo.R;

import java.util.ArrayList;
import java.util.List;

public class AdapterActivity extends AppCompatActivity {

    private List<NewsBean> list = new ArrayList<>();

    private ListView listView;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_adapter);
        setListContent();

        // https://www.jianshu.com/p/23b8ef5c946b

    }
    public void setListContent(){

        listView = new ListView(AdapterActivity.this); // 创建view对象
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        addContentView(listView,layoutParams);


        // /**
        // * 基于传统Holder的传统Adapter  演示
        // *
//        NormalAdapter adapter = new NormalAdapter(getApplicationContext(),new ArrayList<NewsBean>());
//        listView.setAdapter(adapter);



/**
 * 继承通用Adapter且使用通用Holder的适配器
 */
        CommentAdapter adapter = new CommentAdapter<NewsBean>(AdapterActivity.this,list) {
            @Override
            public int getViewLayoutId(int position) {
                return R.layout.layout_adapter_item;
            }

            @Override
            public void setViewContent(CommonViewHolder viewHolder, NewsBean ben) {
                TextView titleView1 = viewHolder.getView(R.id.item_title);
                TextView titleView2 = viewHolder.getView(R.id.item_message);
                TextView titleView3 = viewHolder.getView(R.id.item_time);
                TextView titleView4 = viewHolder.getView(R.id.item_phone);
                titleView1.setText(ben.getTitle());
                titleView2.setText(ben.getMessage());
                titleView3.setText(ben.getTime());
                titleView4.setText(ben.getPhone());
            }
        };

        listView.setAdapter(adapter);
        // 配置数据源
        for (int i = 0; i < 20; i++){
            NewsBean bean = new NewsBean("title" + i,"message" + i,"time" + i,"phone" + i);
            list.add(bean);
        }

        adapter.notifyDataSetChanged();
    }
}
